package cn.broadmax.boot.modules.sys.controller;

import cn.broadmax.boot.base.SuperController;
import cn.broadmax.boot.log.SysLog;
import cn.broadmax.boot.modules.sys.entity.SysDepartment;
import cn.broadmax.boot.base.message.SResult;
import cn.broadmax.boot.modules.sys.bean.depart.SysDepartmentSearch;
import cn.broadmax.boot.modules.sys.mapper.SysDepartmentMapper;
import cn.broadmax.boot.modules.sys.service.ISysDepartmentService;
import cn.broadmax.boot.util.SAssert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Console;

@Api(tags = "部门")
@RestController
@RequestMapping("/sys/department")
@RequiredArgsConstructor
public class SysDepartmentController extends SuperController {

    @Autowired
    private final ISysDepartmentService sysDepartmentService;

    @Autowired
    private final SysDepartmentMapper sysDepartmentMapper;

    @ApiOperation("新增部门")
    @SysLog(value = "新增部门")
    @PostMapping(value = "/insert")
    public SResult<SysDepartment> addDepartment(@RequestBody SysDepartment department){
        SAssert.hasText(department.getDepartName(), "组织名称不能为空");
        SAssert.hasText(department.getOrgCode(), "组织编码不能为空");
        SysDepartment exist = sysDepartmentService.getDepartmentByCode(department.getOrgCode());
        SAssert.isNull(exist, "组织编码已存在");
        sysDepartmentService.save(department);

        department = sysDepartmentService.getById(department.getId());
        return SResult.ok("", department);
    }

    @ApiOperation("根据主键删除组织/部门")
    @SysLog(value = "删除组织/部门")
    @DeleteMapping(value = "/delete/{departmentId}")
    public SResult<Boolean> deleteDepartmentById(@PathVariable String departmentId){
        boolean success = sysDepartmentService.removeById(departmentId);
        return SResult.ok("", success);
    }

    @ApiOperation("根据主键更新部门, 组织编码不能更新")
    @SysLog(value = "更新组织/部门")
    @PutMapping(value = "/update/{departmentId}")
    public SResult<SysDepartment> updateDepartmentById(@PathVariable String departmentId, @RequestBody SysDepartment department){
        System.out.println("-------111111");
        System.out.println(department.toString());
        SAssert.notNull(department);
//        department.setId(departmentId);
//        department.setOrgCode(department.getOrgType());

        sysDepartmentService.updateById(department);

        department = sysDepartmentService.getById(department.getId());
        return SResult.ok("", department);
    }

    @ApiOperation("分页查询部门")
    @GetMapping(value = "")
    public SResult<IPage<SysDepartment>> pageRoles(SysDepartmentSearch search){
        LambdaQueryWrapper<SysDepartment> wrapper = SysDepartmentSearch.createWrap(search);
        IPage<SysDepartment> page = sysDepartmentService.page(getPage(), wrapper);
        return SResult.ok("", page);
    }

}
